package the.formula.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Voltage extends Activity{
		
	private double answer = 0;
        private EditText voltage;
        private String sVoltage;
        private EditText current;
        private String sCurrent;
        private EditText resistance;
        private String sResistance;
        private TableLayout tl;
        private TextView tv;
        private TextView tv1;
        private TextView tv2;
        private boolean answerAdded = false;
        
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voltage_activity);
        tl = (TableLayout)findViewById(R.id.voltageLayout);
        tv = new TextView(this);
        tv1 = new TextView(this);
        tv2 = new TextView(this);
    }
        
    public void onClick(View v) {
        switch(v.getId()) {
        	case R.id.voltageSolve:
        		solveForVoltage();
                break;
        }
    }
            
    private void solveForVoltage() {
        voltage = (EditText)findViewById(R.id.voltageValue);
        current = (EditText)findViewById(R.id.currentValue);
        resistance = (EditText)findViewById(R.id.resistanceValue);
        sVoltage = voltage.getText().toString();
        sCurrent = current.getText().toString();
        sResistance = resistance.getText().toString();
        
        double voltageNum = 0;
        double currentNum = 0;
        double resistanceNum = 0;
        
        if(!sVoltage.equals("")) {
        	voltageNum = Integer.parseInt(sVoltage);
        }
        if(!sCurrent.equals("")) {
        	currentNum = Integer.parseInt(sCurrent);
        }
        if(!sResistance.equals("")) {
        	resistanceNum = Integer.parseInt(sResistance);
        }
        if(sVoltage.equals("")) {
        	voltage(currentNum, resistanceNum);
        	tv1.setText("Current: " + currentNum);
        	tv2.setText("Resistance: " + resistanceNum);
        	tv.setText("Voltage: " + answer);
        } else if(sCurrent.equals("")) {
         	current(voltageNum, resistanceNum);
         	tv1.setText("Voltage: " + voltageNum);
        	tv2.setText("Resistance: " + resistanceNum);
         	tv.setText("Current: " + answer);
        } else if(sResistance.equals("")) {
        	resistance(voltageNum, currentNum);
        	tv1.setText("Voltage: " + voltageNum);
        	tv2.setText("Current: " + currentNum);
        	tv.setText("Resistance: " + answer);
        }
        if(answerAdded) {
        	tl.removeView(tv);
        	tl.removeView(tv1);
        	tl.removeView(tv2);
        }
        tl.addView(tv1);
        tl.addView(tv2);
        tl.addView(tv);
        answerAdded = true;
        voltage.setText("");
        current.setText("");
        resistance.setText("");
        tl.requestFocus();
        }

        
        private void voltage(double current, double resistance) {
        	double result = current * resistance;
        	answer = result;
        }
        
        private void current(double voltage, double resistance) {
        	double result = voltage / resistance;
        	answer = result;
        }
        
        private void resistance(double voltage, double current) {
        	double result = voltage / current;
        	answer = result;
        }
}